﻿using System;
using BehaviorDesigner.Runtime.Tasks.Unity.UnityNavMeshAgent;
using QFramework;
using Sirenix.OdinInspector;
using UnityEngine;

public class BombItem : MonoBehaviour,IController
{
    public IArchitecture GetArchitecture() => GameArchitecture.Interface;

    public float bombSpeed = 300f;
    public bool isStart = false;
    public Collider2D boxCollider2D;
    
    public void Awake()
    {
        boxCollider2D = GetComponent<Collider2D>();
        boxCollider2D.enabled = isStart;
        this.RegisterEvent<GameStart>(OnStart);
    }

    void OnStart(GameStart gameStartEvent)
    {
        boxCollider2D.enabled = true;
        isStart = true;
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (!isStart) return;
        if (collision.gameObject.tag == "Player")
        {
            var audioSystem = this.GetSystem<AudioSystem>();
            audioSystem.PlaySoundEffect("Sound/effects/bomb");
            var position = collision.contacts[0].point;
            var dir = (position - (Vector2)transform.position).normalized;
            var rigidbody2D = collision.gameObject.GetComponent<Rigidbody2D>();
            rigidbody2D.velocity +=   dir * bombSpeed;
            Destroy(this.gameObject);
        }
    }
}
